package com.wehealth;





import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wehealth.Dr_Select;
import com.wehealth.News_List;
import com.wehealth.Plan_Week;
import com.wehealth.Feedback;
import com.wehealth.AboutUs;
import com.wehealth.dm.User;
import com.wehealth.util.DataManger;

public class Menu_Side extends Fragment implements OnItemClickListener{
	
	private  static final String[] aMenuItem=new String[]{"首页","咨询记录","健康查询","在线客服","关于我们","退出登录"};  //"我的收藏","个人信息",
	
	private MainActivity mContext;
	
	private TextView userName;
	private ListView mList;
	private boolean misLogin = false;
	private DataManger mDataManger;
	private User user;
	
	private static final int HOME =0;
	private static final int QUSETION =1;
	private static final int HEALTH_SEARCH =2;
	private static final int ONLINE_SERVER =3;
	private static final int ABOUT_US = 4;
	private static final int LOGOUT =5;
  
    
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
    }  
    
    
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.menu_side, container,false);    
		return view;
		
	}
	

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		mContext = (MainActivity)getActivity();
		mDataManger =mContext.getDataManger();
		mList = (ListView)mContext.findViewById(R.id.lvList);
		
		ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(getActivity(), 
				R.layout.side_list_item, R.id.fanction_name, aMenuItem);
		mList.setAdapter(colorAdapter);
		mList.setOnItemClickListener(this);
		
		View view =(View)mContext.findViewById(R.id.menu_user_info);
		view.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Fragment newContent = null;
				if(misLogin){
					//进入个人信息界面
					 newContent = new Personal_Info();
				}else{
					//进入登录界面
					newContent = new Login();				
				}
				switchFragment(newContent);
			}
		});
		
		updateUserState();
	}

	    
	public void updateUserState(){
		misLogin = mContext.getLoginState();
		user = mContext.getUserInfo();
		userName =(TextView)mContext.findViewById(R.id.user_name);
		ImageView userLogo =(ImageView)mContext.findViewById(R.id.imgUserLogo);
		//用户名的获得，通过登录过来。
		BitmapDrawable drawable;
		if(misLogin){
			userName.setText(user.getAccount());
		   drawable = (BitmapDrawable)getResources().getDrawable(R.drawable.user_logo);
		}else{
			userName.setText("未登录");
			drawable = (BitmapDrawable)getResources().getDrawable(R.drawable.userlog_nologin);
		}
		Bitmap bm =drawable.getBitmap();
		bm =createCircleImage(bm);
		userLogo.setImageBitmap(bm);
		
	}

	// the meat of switching the above fragment
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;
		
		if (getActivity() instanceof MainActivity) {
		
			
			MainActivity activity = (MainActivity) getActivity();
			
			activity.switchContent(fragment);
			
			
			/*
			FragmentManager fragmentManager =getFragmentManager();
			FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
			
			fragmentTransaction.replace(R.id.content_frame, fragment);
			fragmentTransaction.addToBackStack(null);
			//提交修改
			fragmentTransaction.commit();
			*/
			/*
			activity.getSupportFragmentManager()
			.beginTransaction()
			.replace(R.id.content_frame,  fragment)
			.commit();
			*/
			
		} 
		
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		
		Fragment newContent = null;
		
		switch (position) {
		case HOME:
			Log.e("onClick:", "side menu0");
			newContent = new Home();

			break;
			
//		case 1:
//			Log.e("onClick:", "side menu1");
//			//newContent = new Dr_Select();
//
//			//break;
//			
		case QUSETION:
			newContent = new Question_List();
			break;
			
		case HEALTH_SEARCH:
			newContent = new Health_search_Select();
			break;
			
			
		case ONLINE_SERVER:
			newContent = new Feedback();

			break;
			
		case ABOUT_US:
			newContent = new AboutUs();

			break;
		case LOGOUT:
			openLogoutDialog();
			break;
		}
		
		if (newContent != null)
		{
			switchFragment(newContent);
		}
	}
	
	
    private Bitmap createCircleImage(Bitmap bitmap)  
    {  
        final Paint paint = new Paint();  
        paint.setAntiAlias(true);  
        Bitmap target = Bitmap.createBitmap(bitmap.getHeight(),bitmap.getWidth(),Config.ARGB_8888);
        /** 
         * 产生一个同样大小的画布 
         */  
        Canvas canvas = new Canvas(target);  
        /** 
         * 首先绘制圆形 
         */  
        Rect rect=new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
        RectF rectF=new RectF(rect);
        canvas.drawOval(rectF, paint);
        /** 
         * 使用SRC_IN，参考上面的说明 
         */  
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
        /** 
         * 绘制图片 
         */  
        canvas.drawBitmap(bitmap, 0, 0, paint);  
        return target;  
    }  
    
    
    private void openLogoutDialog(){
 	   AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
 	   builder.setIcon(null);
 	   String str = getString(R.string.ask_logout_warnning);
 	    TextView textview = new TextView(mContext);
 	    textview.setText(str);
 	    textview.setTextColor(Color.BLACK);
 	    textview.setTextSize(18);

 	    textview.setGravity(Gravity.CENTER);
 	    builder.setView(textview);
 	    
 	    
 	    builder.setPositiveButton("确定", new AlertDialog.OnClickListener() {
 			@Override
 			public void onClick(DialogInterface arg0, int arg1) {
 				// TODO Auto-generated method stub
 				mDataManger.delUserDate();
 				updateUserState();
 			}
 	    	
 	    });
 	    
 	   builder.setNegativeButton("取消", null);
 	    builder.create().show();
 		
 	}
 	
    
    
    
    
}
